<template>
  <div class="login-container">
    <div class="login-wrap">
      <div class="login-left">
        <p class="login-left-txt">管理后台登录</p>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
          size="small"
        >
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="off"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              ref="password"
              v-model="loginForm.password"
              type="password"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="off"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="submit-button"
            @click.native.prevent="handleLogin"
          >登 录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  mounted() {

  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          try {
            await this.$store.dispatch('user/login', this.loginForm)
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
            // this.$store.dispatch('user/getVideoPlayWay')
          } catch (error) {
            this.loading = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    margin: 25px 0;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background: linear-gradient(
    180deg,
    rgba(25, 43, 83, 1) 0%,
    rgba(11, 19, 41, 1) 100%
  );

  .login-wrap {
    display: flex;
    overflow: hidden;
    align-content: center;
    align-items: center;
    justify-content: center;
    margin: 0;
    padding: 0;
    height: 100vh;
  }

  .login-left {
    width: 400px;
    height: 344px;
    background-color: rgba(28, 41, 76, 1);
    box-sizing: border-box;
  }

  .login-left-txt {
    box-sizing: border-box;
    color: #ffffff;
    padding: 0 30px;
    font-size: 13px;
  }

  .login-form {
    position: relative;
    width: 400px;
    height: 326px;
    overflow: hidden;
    background-color: rgba(36, 51, 93, 1);
    box-sizing: border-box;
    padding: 36px 40px;
  }
  .submit-button{
    width: 100%;
  }
}
::v-deep .el-input__inner {
  &::placeholder {
    color: rgba(255, 255, 255, 0.2);
  }
}
</style>
